package cn.vacuumflask.flaskutils;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;

import cn.vacuumflask.flaskutils.adapter.CommonAdapter;
import cn.vacuumflask.flaskutils.adapter.OnRecycleViewItemClickListener;
import cn.vacuumflask.flaskutils.adapter.OnRecycleViewItemLongClickListener;
import cn.vacuumflask.flaskutils.databinding.ActivityMainBinding;
import cn.vacuumflask.flaskutils.databinding.ItemStudentBinding;
import cn.vacuumflask.flaskutils.entity.Student;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.rvDetail.setLayoutManager(new LinearLayoutManager(this));
        ArrayList<Student> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Student("张三 — " + i, i + 20));
        }
        CommonAdapter<Student> adapter = new CommonAdapter<Student>(list) {

            @Override
            protected CommonViewHolder setViewHolder(ViewGroup viewGroup, int viewType) {
                ItemStudentBinding binding = DataBindingUtil.inflate(LayoutInflater.from(MainActivity.this), R.layout.item_student, viewGroup, false);
                return new CommonViewHolder<ItemStudentBinding>(binding.getRoot(), binding);
            }

            @Override
            protected void setBindViewHolder(CommonViewHolder viewHolder, int position) {
                Student stu = data.get(position);
                ((ItemStudentBinding) viewHolder.binding).tvName.setText("姓名：" + stu.getName());
                ((ItemStudentBinding) viewHolder.binding).tvAge.setText("年龄：" + stu.getAge());

            }
        };

        adapter.setItemClickListener(new OnRecycleViewItemClickListener<Student>() {
            @Override
            public void onRecycleViewItemClickListener(View itemView, Student entity, int position) {
                Toast.makeText(MainActivity.this, "点击：" + entity.getName(), Toast.LENGTH_SHORT).show();
            }
        });

        adapter.setLongClickListener(new OnRecycleViewItemLongClickListener<Student>() {
            @Override
            public boolean onRecycleViewItemLongClickListener(View itemView, Student entity, int position) {
                Toast.makeText(MainActivity.this, "长按：" + entity.getName(), Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        binding.rvDetail.setAdapter(adapter);
        //添加分割线
        binding.rvDetail.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }
}
